import cv2
import numpy as np


color_lower = np.array([65, 130, 60], np.uint8) 
color_upper = np.array([77, 170, 150], np.uint8) 

while True:
    max_x = 0
    max_center = -1

    color_frame = None #frame

    # Convert images to numpy arrays
    color_image = np.asanyarray(color_frame.get_data())
    hsvFrame = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
    color_mask = cv2.inRange(hsvFrame, color_lower, color_upper)

    kernal = np.ones((5, 5), "uint8") 
    color_mask = cv2.dilate(color_mask, kernal) 
    res = cv2.bitwise_and(color_image, color_image, mask = color_mask) 

    # Creating contour to track red color 
    contours, hierarchy = cv2.findContours(color_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    area = max(contours,key = cv2.contourArea)
    x, y, w, h = cv2.boundingRect(area)
    color_image = cv2.rectangle(color_image, 
                            (x, y), 
                            (x + w, y + h), 
                            (0, 0, 255), 2)

    x_center = x + int(w/2)
    y_center = y + int(h/2)
    cv2.imshow("Color Detection in Real-Time", color_image)
    
    waitkey = cv2.waitKey(1)
    if waitkey==ord('q'): break

cv2.destroyAllWindows()
